﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using Lime49;
using Lime49.Mobile.UI;
using LockCrypt.DAL;
using LockCrypt.Core;

namespace LockCrypt.Mobile {
    /// <summary>
    /// A form which allows the adding/editing of groups.
    /// </summary>
    public partial class FrmEditGroup : NonFullscreenForm {
        private List<IGroup> groups;
        BindingSource bsGroups;

        /// <summary>
        /// Gets or sets the group being edited.
        /// </summary>
        /// <value>The group.</value>
        public IGroup Group { get; set; }

        /// <summary>
        /// Initializes event listeners and populates the Parent Group dropdown.
        /// </summary>
        private FrmEditGroup() {
            InitializeComponent();
            groups = DalConfigurationManager.Provider.GetGroups().ToList();
            groups.Insert(0, new GroupBase() {
                Name = I18nUtils.GetString("Strings", "None"),
                ID = null,
                ParentID = null
            });
            bsGroups = new BindingSource() {
                DataMember = "ID"
            };
            foreach(IGroup grp in groups) {
                bsGroups.Add(grp);
            }
            ddParentGroup.DisplayMember = "Name";
            ddParentGroup.ValueMember = "ID";
            ddParentGroup.DataSource = bsGroups;
            ddParentGroup.SelectedIndex = 0;
            btnCancel.Click += (s, e) => {
                this.DialogResult = DialogResult.Cancel;
            };
            btnOk.Click += (s, e) => {
                if(ValidateGroup()) {
                    Group.Name = txtGroupName.Text;
                    string parent = Convert.ToString(ddParentGroup.SelectedValue);
                    Group.ParentID = string.IsNullOrEmpty(parent) ? null : parent;
                    //string selectedGroup = ddParentGroup.SelectedItem.ToString();
                    //Group.ParentID = groups.First(g => g.Name == selectedGroup).ID;
                    this.DialogResult = DialogResult.OK;
                }
            };
            Group = new GroupBase();
        }

        /// <summary>
        /// Initializes a new <see cref="FrmEditGroup"/> to add a group.
        /// </summary>
        /// <param name="parentId">The parent group ID.</param>
        public FrmEditGroup(string parentId)
            : this() {
            this.Text = I18nUtils.GetString("Strings", "AddGroup");
            btnOk.Text = I18nUtils.GetString("Strings", "Add");
            //ddParentGroup.SelectedValue = groups.FirstOrDefault(g => g.ID == parentId) ?? groups.First();
            if(parentId != null) {
                ddParentGroup.SelectedValue = parentId;
            }
        }

        /// <summary>
        /// Initializes a new <see cref="FrmEditGroup"/> to edit a group.
        /// </summary>
        /// <param name="group">The group.</param>
        public FrmEditGroup(IGroup group)
            : this() {
            this.Group = group;
            txtGroupName.Text = group.Name;
            var arrangedGroups = LockCryptBLL.ArrangeGroups(groups.Skip(1));
            IGroup groupWithChildren = null;
            foreach(var grp in arrangedGroups) {
                groupWithChildren = grp.FindGroup(group.ID);
                if(groupWithChildren != null)
                    break;
            }
            if(groupWithChildren != null) {
                IEnumerable<IGroup> descendants = groupWithChildren.Flatten();
                foreach(var childGroup in descendants) {
                    groups.Remove(childGroup);
                    bsGroups.Remove(childGroup);
                    //ddParentGroup.Items.Remove(childGroup.Name);
                }
            }
            //ddParentGroup.SelectedItem = group.Name;
            //ddParentGroup.SelectedValue = group.ID;
            ddParentGroup.SelectedValue = group.ID;
        }

        /// <summary>
        /// Checks that the group name is not null and that no group with the name entered exists. Displays a messagebox if errors are detected.
        /// </summary>
        /// <returns></returns>
        private bool ValidateGroup() {
            if(string.IsNullOrEmpty(txtGroupName.Text)) {
                MessageBox.Show(I18nUtils.GetString("Errors", "GroupNameRequired"), I18nUtils.GetString("Errors", "Error"), MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
                txtGroupName.SelectAll();
                txtGroupName.Focus();
                return false;
            }
            return true;
        }
    }
}